<?php

    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2015/5/27 0027
     * Time: 上午 2:27
     */
    class Layout
    {

        var $obj;
        static public $data=[];
        var $layout_path = '';
        var $layout;
        static public $layout_view=[];

        /*function Layout( $layout = "admin_layout",$data=null )
        {
            self::$data=$data;
            //self::$data=array_merge(self::$data,$data);
            $this->obj =& get_instance();
            $this->layout = $layout;
        }*/
        function Layout( array $layout,$data=null )
        {
            if(self::$data)
            {
                $data=array_merge(self::$data,$data);
            }else{
                self::$data=$data;
            }

            //self::$data=array_merge(self::$data,$data);
            $this->obj =& get_instance();
            $this->layout = $layout;
        }

        function setLayout( $layout )
        {
            $this->layout = $layout;
        }

        function view( $view, $data = [], $return = false )
        {
            if(self::$data)
            {
                $data=array_merge(self::$data,$data);
            }

            $data[ 'content_for_layout' ] = $this->obj->load->view( $view, $data, true );
            if ( $return ) {
                $output = $this->obj->load->view( $this->layout, $data, true );

                return $output;
            } else {
                //$this->obj->load->view( $this->layout, $data, false );
                if ( strtolower( substr( $this->obj->router->directory, 0, -1 ) ) == 'admin' ) {
                    $this->obj->load->view( $this->layout, $data, false );
                } else {
                    $data[ 'model_for_layout' ] = $this->obj->load->view( $this->layout, $data, true );
                    $this->obj->load->view( "global_layout", $data, false );
                }
            }

        }
    }